package com.itheima.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.itheima.dao.UploadFileDao;
import com.itheima.po.UploadFile;

@Service
public class FilenameService {
	@Autowired
	private UploadFileDao uploadFileDao;
	
	public String addUploadFilename(String name,String originalFilename) {
		String suffix = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
		String prefix = originalFilename.substring(0,originalFilename.lastIndexOf("."));
		String newfilename = prefix+"-"+name+"."+suffix;
		
		UploadFile uploadfile = new UploadFile();
		uploadfile.setUsername(name);
		uploadfile.setFilename(newfilename);
		this.uploadFileDao.addUploadFile(uploadfile);
		
		return newfilename;
	}
	
	public List<UploadFile> getAllUploadFile(){
		return this.uploadFileDao.findAllUploadFile();
	}
	
	public String getUploadFilename(String username,String filename) {
		String suffix = filename.substring(filename.lastIndexOf(".") + 1);
		String prefix = filename.substring(0,filename.lastIndexOf("."));
		String newfilename = prefix+"-"+username+"."+suffix;
		return newfilename;		
	}
}
